<?php
namespace app\admin\controller;
use think\Controller;
use customtool\Utils;

// 数据统计
class Stats extends Base
{

    // 收款订单列表
    public function orderList() {
        //商家列表
        $members = db('pay_bis')->group('merchant_id')->column('merchant_id');

        /* 时间段 */
        $datetype = input('datetype');
        switch ($datetype) {
            case 'wk'://最近一周数据
                $starttime = date('Y-m-d', strtotime('-6days'));
                $endtime = date('Y-m-d');
                $stime = strtotime($starttime);
                $etime = strtotime(date('Y-m-d', strtotime('+1days')));
                break;
//            case 'tdy': //今日数据
//                $starttime = date('Y-m-d');
//                $endtime = date('Y-m-d');
//                $stime = strtotime($starttime);
//                $etime = strtotime("+1days", strtotime($endtime));
//                break;
            case 'ydy': //昨日数据 
                $starttime = date('Y-m-d', strtotime("-1days"));
                $endtime = date('Y-m-d', strtotime("-1days"));
                $stime = strtotime($starttime);
                $etime = strtotime("+1days", strtotime($endtime));
                break;
            default: //默认当天数据或有时间段查询操作
                $stime = input('stime', 0);
                $etime = input('etime', 0);
//                $shour = input('shour', 0);
//                $ehour = input('ehour', 0);
                if (($stime == 0 && $etime == 0) || ($stime == $etime && $stime == date('Y-m-d'))) {
                    $datetype = "tdy";
                } elseif ($stime == $etime && $stime == date('Y-m-d', strtotime("-1days"))) {
                    $datetype = "ydy";
                } elseif ($stime == date('Y-m-d', strtotime("-6days")) && $etime == date('Y-m-d')) {
                    $datetype = "wk";
                }
                $starttime = $stime != 0 ? $stime : date('Y-m-d');
                $endtime = $etime != 0 ? $etime : date('Y-m-d');
                if ($stime != 0) {
                    $stime = strtotime($stime); //$stime.' '.$shour
                    $etime = strtotime("+1days", strtotime($etime));
                    ; //$etime.' '.$ehour
                } else {
                    $stime = strtotime(date('Y-m-d'));
                    $etime = strtotime(date('Y-m-d', strtotime('+1days')));
                }
                break;
        }
//        $sql = " and paytime >= ".$stime." and paytime <= ".$etime;
        $map = array(); //查询条件
        $map['create_time'] = ['between', [$stime, $etime]];
//        $map = array_merge($map,['create_time' => ['between',[$stime,$etime]]]);
//        return  json($map);
        //增加筛选条件，商户\支付类型
//        $paytype = input('paytype');
        $merchant_id = input('merchant_id');
//        if($paytype == 1){
//            $sql .= " and pay_way='weixin'";
//        }elseif ($paytype == 2) {
//            $sql .= " and pay_way='alipay'";
//        }
        if (!!$merchant_id) {
            $map['merchant_id'] = ['=', $merchant_id];
        }
        $status = input('status', 0); //默认显示全部状态数据
        if ($status != 0) {
            $map['status'] = ['=', $status];
        }

//        $m = D('Manage/Order');
//        //判断是商户还是员工登陆
//        if(empty($usId)){//商户
//            $orders = $m->getAll(session('SX_USERS.userId'),$limit=0,$model=1,$sql);//根据用户ID
//            $countIncome = $m->countIncome(session('SX_USERS.userId'),$sql,$model=1);
//        }else{
//            $orders = $m->getAll(session('SX_USERS.storeId'),$limit=0,$model=3,$sql);//根据店铺ID
//            $countIncome = $m->countIncome(session('SX_USERS.storeId'),$sql,$model=3);
//        }
//        $s = D('Manage/Stores');
//        $stores = $s->get(session('SX_USERS.userId'));
//        $u = D('Manage/Ustaffs');
//        $ustaffs = $u->get(session('SX_USERS.userId'));
//
//        foreach ($stores as $key => $value) {
//            $store[$value['storeId']] = $value['business_name'].$value['branch_name'];
//        }
//
//        foreach ($ustaffs as $key => $value) {
//            $ustaff[$value['usId']] = $value['userName'];
//        }
//        
        //符合条件的订单
        $orderlist = db('pay_bis_order')->where($map)->select();

        //订单统计
        $totalAmount = 0;
        $totalFee = 0;
        $totalActualAmount = 0;
        $totalNum = 0;
//        $weixinMoney = 0;
//        $weixinNum = 0;
//        $alipayMoney = 0;
//        $alipayNum = 0;        
//        $refund_fee = 0;
//        $refund_num = 0;
        foreach ($orderlist as &$value) {
            $value['pay_amount'] /= 100;
            $value['fee'] = ($value['pay_fee']+$value['mchnt_fee'])/100;
            $value['actual_amount'] /= 100;
//            if($value['pay_way'] == 'weixin' and $value['ispay'] == 1 and $value['refund'] == 0){
//                $weixinMoney += $value['goods_price'];
//                $weixinNum += 1;
//            }elseif($value['pay_way'] == 'alipay' and $value['ispay'] == 1 and $value['refund'] == 0){
//                $alipayMoney += $value['goods_price'];
//                $alipayNum += 1;
//            }else{
//                if($value['status'] != 0){
//                    if(0 != $value['refund']){
//                        $refund_fee += $value['refund_fee'];
//                        $refund_num += 1;
//                    }
            $totalAmount += $value['pay_amount'];
            $totalFee += $value['fee'];
            $totalActualAmount += $value['actual_amount'];
            $totalNum += 1;
//                }                
//            }            
        }
////        $countIncome = $countIncome+$totalAmount-$refund_fee;
//        $totalAmount += $weixinMoney + $alipayMoney;
//        $totalNum += $weixinNum + $alipayNum;


        $this->assign('members', $members);
//        $this->assign('mendians', $stores);
        $this->assign('merchant_id', $merchant_id);
        $this->assign('status', $status);
//        $this->assign('paytype', $paytype);
        $this->assign('datetype', $datetype);
        $this->assign('starttime', $starttime);
        $this->assign('endtime', $endtime);
////        $this->assign('shour', $shour);
////        $this->assign('ehour', $ehour);
//        $this->assign('countIncome',$countIncome);
//        $this->assign('orders',$orders);
//        $this->assign('store',$store);
//        $this->assign('ustaff',$ustaff);        
        $this->assign('totalAmount', $totalAmount);
        $this->assign('totalFee', $totalFee);
        $this->assign('totalActualAmount', $totalActualAmount);
        $this->assign('totalNum', $totalNum);
//        $this->assign('weixinMoney', $weixinMoney);
//        $this->assign('weixinNum', $weixinNum);
//        $this->assign('alipayMoney', $alipayMoney);
//        $this->assign('alipayNum', $alipayNum);
//        }
//        
//        $orderlist = db('app_bis_order') -> select();
        $this->assign('orderlist', $orderlist);
        return $this->fetch();
    }

    //订单详情
    public function orderDetail() {
        $oid = input('pay_orderid');
        if (!!$oid) {
            $order = db('pay_bis_order')->where('pay_orderid', $oid)->find();
            $order['pay_amount'] /= 100;
            $order['fee'] = ($order['pay_fee']+$order['mchnt_fee'])/100;
            $order['actual_amount'] /= 100;
            $this->assign('order', $order);
            return $this->fetch();
        } else {
            return '无订单号，请重试';
        }
    }

    //代付代发记录
    public function withdrawal() {
        //商家列表
        $members = db('pay_bis')->group('id')->column('id');

        /* 时间段 */
        $datetype = input('datetype');
        switch ($datetype) {
            case 'wk'://最近一周数据
                $starttime = date('Y-m-d', strtotime('-6days'));
                $endtime = date('Y-m-d');
                $stime = strtotime($starttime);
                $etime = strtotime(date('Y-m-d', strtotime('+1days')));
                break;
//            case 'tdy': //今日数据
//                $starttime = date('Y-m-d');
//                $endtime = date('Y-m-d');
//                $stime = strtotime($starttime);
//                $etime = strtotime("+1days", strtotime($endtime));
//                break;
            case 'ydy': //昨日数据 
                $starttime = date('Y-m-d', strtotime("-1days"));
                $endtime = date('Y-m-d', strtotime("-1days"));
                $stime = strtotime($starttime);
                $etime = strtotime("+1days", strtotime($endtime));
                break;
            default: //默认当天数据或有时间段查询操作
                $stime = input('stime', 0);
                $etime = input('etime', 0);
//                $shour = input('shour', 0);
//                $ehour = input('ehour', 0);
                if (($stime == 0 && $etime == 0) || ($stime == $etime && $stime == date('Y-m-d'))) {
                    $datetype = "tdy";
                } elseif ($stime == $etime && $stime == date('Y-m-d', strtotime("-1days"))) {
                    $datetype = "ydy";
                } elseif ($stime == date('Y-m-d', strtotime("-6days")) && $etime == date('Y-m-d')) {
                    $datetype = "wk";
                }
                $starttime = $stime != 0 ? $stime : date('Y-m-d');
                $endtime = $etime != 0 ? $etime : date('Y-m-d');
                if ($stime != 0) {
                    $stime = strtotime($stime); //$stime.' '.$shour
                    $etime = strtotime("+1days", strtotime($etime));
                    ; //$etime.' '.$ehour
                } else {
                    $stime = strtotime(date('Y-m-d'));
                    $etime = strtotime(date('Y-m-d', strtotime('+1days')));
                }
                break;
        }
        
        $map = array(); //查询条件
        $map['create_time'] = ['between', [$stime, $etime]];
        $merchant_id = input('merchant_id');
        if (!!$merchant_id) {
            $map['merchant_id'] = ['=', $merchant_id];
        }
        $status = input('status', 999); //默认显示全部数据
        if ($status != 999) {
            $map['status'] = ['=', $status];
        }
        $bank_status = input('bank_status', 'all'); //默认显示全部数据
        if ($bank_status != 'all') {
            $map['bank_status'] = ['=', $bank_status];
        }
        
        //符合条件的订单
        $withdrawal = db('pay_daifu_apply')->where($map)->order('create_time', 'desc')->select();

        //订单统计
        $totalAmount = 0;
        $totalFee = 0;
        $totalNum = 0;
        foreach ($withdrawal as &$value) {
            $value['amount'] /= 100;
            $value['fee'] /= 100;
            $totalAmount += $value['amount'];
            $totalFee += $value['fee'];
            $totalNum += 1;
        }


        $this->assign('members', $members);
        $this->assign('merchant_id', $merchant_id);
        $this->assign('status', $status);
        $this->assign('bank_status', $bank_status);
        $this->assign('datetype', $datetype);
        $this->assign('starttime', $starttime);
        $this->assign('endtime', $endtime);
        $this->assign('totalAmount', $totalAmount);
        $this->assign('totalFee', $totalFee);
        $this->assign('totalNum', $totalNum);
        $this->assign('withdrawal', $withdrawal);
        return $this->fetch();
    }

    //代付代发详情
    public function withdrawalDetail() {
        $oid = input('mchnt_batchid', 0);
        $draw = db('pay_daifu_apply')->where('mchnt_batchid', $oid)->find();
        $this->assign('draw', $draw);
        return $this->fetch();
    }
    
    //代付代发审核
    public function auditing() {
        $data = input('post.');
        //判断是否已被审核
        $is_agree = db('pay_daifu_apply')->where('id', $data['id'])->column('is_agree');
        if($is_agree){
            $result = [
                'code'=> FALSE,
                'msg'=> '该申请已被审核！'
            ];
        } else {
            $url = 'http://39.107.87.17/pay/common/admin_daifu/index';
            $result = Utils::curl($url, $data);
            
//            $event = new \pay\common\admin_daifu\index;
//            $result = $event->index($url, $data);
        }
        return $result;
    }

}
